import 'package:flutter/material.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/toast_utils.dart';

class SearchBox extends StatefulWidget {
  final String hintText;
  final IconData searchIcon;
  final Color borderColor;
  final double borderRadius;
  final Color backgroundColor;
  final Color hintTextColor;
  final TextInputType inputType; // 输入类型：text/number
  final ValueChanged<String>? onSubmitted; // 提交回调

  const SearchBox({
    Key? key,
    this.hintText = '搜索',
    this.searchIcon = Icons.qr_code_scanner,
    this.borderColor = Colors.grey,
    this.borderRadius = 6.0,
    this.backgroundColor = Colors.white,
    this.hintTextColor = Colors.grey,
    this.inputType = TextInputType.text, // 默认为文本输入
    this.onSubmitted,
    this.focusNode,
  }) : super(key: key);

  // 4. 接收外部传递的焦点控制器
  final FocusNode? focusNode;

  @override
  _SearchBoxState createState() => _SearchBoxState();
}

class _SearchBoxState extends State<SearchBox> {
  late TextEditingController _controller;
  String? _errorText;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  bool _validateInput() {
    final text = _controller.text;

    if (text.isEmpty) {
      Toast.show('请输入内容');
      return false;
    }

    if (widget.inputType == TextInputType.number) {
      final numericRegex = RegExp(r'^\d+$');
      if (!numericRegex.hasMatch(text)) {
        Toast.show('请输入有效电话');
        return false;
      }
    }

    setState(() {
      _errorText = null;
    });
    return true;
  }

  void _submitInput() {
    if (_validateInput()) {
      widget.onSubmitted?.call(_controller.text);
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      // 5. 接收外部传递的焦点控制器
      focusNode: widget.focusNode,
      controller: _controller,
      keyboardType: widget.inputType,
      decoration: InputDecoration(
        prefixIcon: Icon(
          widget.searchIcon,
          color: Colors.grey,
          size: 18.0,
        ),
        hintText: widget.hintText,
        hintStyle: TextStyle(color: widget.hintTextColor, fontSize: 14.0),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(widget.borderRadius),
          borderSide: BorderSide(
            color: Colours.dark_red,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(widget.borderRadius),
          borderSide: BorderSide(
            color: Colours.orange,
          ),
        ),
        // enabledBorder: OutlineInputBorder(
        //   borderRadius: BorderRadius.circular(widget.borderRadius),
        //   borderSide: BorderSide(
        //     color: Colours.dark_red,
        //   ),
        // ),
        filled: true,
        fillColor: widget.backgroundColor,
        contentPadding: const EdgeInsets.symmetric(horizontal: 12.0),
      ),
      onSubmitted: (_) => _submitInput(),
    );
  }
}

